//
//  SHNewfeatureViewController.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/22.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
private let NewfeatureViewCellId = "cellId"

class SHNewfeatureViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }

    private func setupUI(){
        
        view.addSubview(collectionView)
        
        collectionView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        
        layout.minimumInteritemSpacing = 0

        layout.minimumLineSpacing = 0

        layout.scrollDirection = .Horizontal
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        view.pagingEnabled = true
        view.bounces = false
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.backgroundColor = RandomColor()
        view.registerClass(SHNewfeatureViewCell.self, forCellWithReuseIdentifier: NewfeatureViewCellId)
        view.delegate = self
        view.dataSource = self

        return view
        
    }()
}

extension SHNewfeatureViewController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewfeatureViewCellId, forIndexPath: indexPath) as! SHNewfeatureViewCell
        
        cell.imageIndex = indexPath.item
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / SCREENW)
        
        if index == 3 {
            
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! SHNewfeatureViewCell
            
            cell.startAnims()
            
        }
    }
    
}
class SHNewfeatureViewCell: UICollectionViewCell {
    
    var imageIndex: Int = 0{
        
        didSet{
            
            bigImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startAnims(){
        
        startButton.hidden = false
        
        self.startButton.transform = CGAffineTransformMakeScale(0, 0)
        self.startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    
    func buttonClick(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "newFeature")
    }
    
    func setupUI(){
        
        backgroundColor = RandomColor()
        
        contentView.addSubview(bigImageView)
        contentView.addSubview(startButton)
        
        bigImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        
        self.startButton.hidden = true
        
    }
    
    
    private lazy var bigImageView: UIImageView = UIImageView()
    
    private lazy var startButton: UIButton = {
       
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        button.addTarget(self, action:"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("欢迎回来", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(18)
        
        button.sizeToFit()
        
        return button
    }()
}